/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.mhstay.slayer.service.impl;

import java.util.Date;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.ServiceContext;
import com.mhstay.slayer.model.House;
import com.mhstay.slayer.service.base.HouseLocalServiceBaseImpl;

/**
 * The implementation of the house local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.mhstay.slayer.service.HouseLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Ahmed Hasan
 * @see com.mhstay.slayer.service.base.HouseLocalServiceBaseImpl
 * @see com.mhstay.slayer.service.HouseLocalServiceUtil
 */
public class HouseLocalServiceImpl extends HouseLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.mhstay.slayer.service.HouseLocalServiceUtil} to access the house local service.
	 */
	
	public House updateHomeStay(long houseId, int houseType, String catchPhrase,
			String houseFamilyDesc, String houseRules, String accomodates,
			ServiceContext serviceContext) {
		
		House house = null;
		
		if (houseId > 0l) {
			try {
				house = fetchHouse(houseId);
				house.setModifiedDate(new Date());
			} catch (SystemException e) {
				e.printStackTrace();
			}
		} else {
			try {
				houseId = counterLocalService.increment(House.class.getName());
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			house = createHouse(houseId);
			house.setCreateDate(new Date());
			try {
				house = addHouse(house);
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
		
		house.setHouseType(houseType);
		house.setCatchPhrase(catchPhrase);
		house.setHouseFamilyDesc(houseFamilyDesc);
		house.setHouseRules(houseRules);
		house.setAccomodates(accomodates);
		
		house.setUserId(serviceContext.getUserId());
		house.setCompanyId(serviceContext.getCompanyId());
		
		try {
			house = updateHouse(house);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return house;
	}
}